﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

using Db4objects.Db4o;
using Db4objects.Db4o.Query;

namespace HoldemGame
{
    class ListDBAdapter
    {
        private Database DB;

        public ListDBAdapter(Database db)
        {
            this.DB = db;
        }

        public int Query(IQuery q, IList output)
        {
            IObjectSet rs = q.Execute();
            for (int i = 0; i < rs.Count; i++)
            {
                output.Add(rs[i]);
            }
            return rs.Count;
        }

        public void Update(object obj)
        {
            DB.DB.Store(obj);
        }

        public void Connect(INotifyCollectionChanged source)
        {
            source.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
        }

        void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (object obj in e.NewItems)
                    Update(obj);
            }
        }
    }
}
